package com.lgs.xlistviewtest.view.ui;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;

import com.lgs.base.BaseActivity;
import com.lgs.base.ToolBarModel;
import com.lgs.common.widgets.xlist.BaseListAdapter;
import com.lgs.common.widgets.xlist.IOnClickItemListener;
import com.lgs.common.widgets.xlist.XListView;
import com.lgs.xlistviewtest.R;
import com.lgs.xlistviewtest.module.bean.DataItem;
import com.lgs.xlistviewtest.presenter.XListPresenter;
import com.lgs.xlistviewtest.view.interfaces.IXListView;
import com.lgs.xlistviewtest.view.item.XListItem;

import java.util.ArrayList;
import java.util.List;
public class XListActivity extends BaseActivity<XListPresenter> implements IXListView, IOnClickItemListener, XListView.IXListViewListener, XListView.OnItemClickListener {
    private XListView mListView;// 表格对象
    private BaseListAdapter<DataItem> mAdapter;// 适配器

    private List<DataItem> srcTable;
    private int readTag = 0;// 0首次，1中间，2结束
    private int curPos = 0;// 当前位置
    private final static int numPerTime = 20;// 每次读取数

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_xlist);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_xlist;
    }

    @Override
    protected void setToolbarTitle(ToolBarModel toolBarModel) {
        super.setToolbarTitle(toolBarModel);
        toolBarModel.centerText = getString(R.string.app_name);
    }

    @Override
    protected void initView() {
        mListView = findViewById(R.id.xListView);
        srcTable = new ArrayList<>();
        getData();

        mAdapter = new BaseListAdapter<>(this, R.layout.list_item_data_bind, XListItem.class,srcTable);// 数据来源
        mListView.setAdapter(mAdapter);// 添加并且显示

    }

    @Override
    protected void addEvents() {
        mListView.setXListViewListener(this);
       // mListView.setOnItemClickListener(this);
        mAdapter.setOnClickItemListener(this);

    }

    private void getData() {
        if (curPos > 100) {
            readTag = 2;
        }
        if (readTag == 2) {
            return;
        }

        for (int i = 0; i < numPerTime; i++) {
            srcTable.add(new DataItem("" + (curPos + i) + " 名字"));
        }
        curPos += numPerTime;
    }

    @Override
    public void onRefresh() {

        if (!mListView.getDataUpdating()) {
            mListView.setDataUpdating(true);
            srcTable.clear();
            readTag = 0;// 0首次，1中间，2结束
            curPos = 0;// 当前位置
            getData();
            mListView.setDataUpdating(false);
            mAdapter.notifyDataSetChanged(); // 更新数据
        }
    }

    @Override
    public void onLoadMore() {
        if (!mListView.getDataUpdating()) {
            mListView.setDataUpdating(true);
            getData();
            mListView.setDataUpdating(false);
            mAdapter.notifyDataSetChanged(); // 更新数据
        }
    }

    @Override
    public void clickItem(View v) {
        int position = (Integer) v.getTag(R.id.tag_row);
        //int field = (Integer)v.getTag(R.id.tag_col);
        DataItem item = srcTable.get(position);
        String name = item.getName();
        Log.d("", "selected is " + name);

    }

    //// 表格行单击处理
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        int pos = position - 1;// 显示所选Item的ItemView的行数
        if (pos < 0 || position >= srcTable.size() )
            return;
        DataItem item = srcTable.get(position);
        String name = item.getName();
        Log.d("", "selected is " + name);
    }
}
